﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Security.Principal;
using System.Collections.Specialized;
using System.ServiceModel;
using System.Web.Configuration;
using System.Diagnostics;

namespace Freshome.d
{
    public static class Editool
    {
        public static WindowsPrincipal GetWindowsPrincipal()
        {
            WindowsPrincipal wp = new WindowsPrincipal(ServiceSecurityContext.Current.WindowsIdentity);
            NameValueCollection appSettings = WebConfigurationManager.AppSettings as NameValueCollection;
            if (appSettings != null)
            {
                if (appSettings.AllKeys.Contains("Junior") && wp.IsInRole(appSettings["Junior"]))
                {
                    return wp;
                }
                else throw new FaultException(string.Format("用户无修改权限！"));
            }
            else throw new FaultException(string.Format("服务端配置错误！"));
        }
        public static void Save<T>(Action<T> action, T arg, WindowsPrincipal wp)   where T : b.Shortype
        {
            try
            {
                action(arg);
                using (var logger = new EventLog("Kingate"))
                {
                    logger.Source = "Freshome";
                    if (EventLog.Exists(logger.Log) && EventLog.SourceExists(logger.Source))
                    {
                        logger.WriteEntry(string.Format("{0} updated {1} (Mid = {2})", wp.Identity.Name, arg.GetType().Name, arg.Mid), EventLogEntryType.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                using (var logger = new EventLog("Kingate"))
                {
                    logger.Source = "Freshome";
                    if (EventLog.Exists(logger.Log) && EventLog.SourceExists(logger.Source))
                    {
                        while (ex.InnerException != null)
                        {
                            ex = ex.InnerException;
                        }
                        logger.WriteEntry(ex.Message, EventLogEntryType.Error);
                    }
                }
                throw new FaultException(ex.Message);
            }
        }
        public static bool Dele<T>(Func<T, bool> funcall, T arg, string typeName) where T : struct
        {
            WindowsPrincipal wp = new WindowsPrincipal(ServiceSecurityContext.Current.WindowsIdentity);
            NameValueCollection appSettings = WebConfigurationManager.AppSettings as NameValueCollection;
            if (appSettings != null)
            {
                if (appSettings.AllKeys.Contains("Junior") && wp.IsInRole(appSettings["Junior"]))
                {
                    try
                    {
                        bool rt = funcall(arg);
                        using (var logger = new EventLog("Kingate"))
                        {
                            logger.Source = "Freshome";
                            if (EventLog.Exists(logger.Log) && EventLog.SourceExists(logger.Source))
                            {
                                logger.WriteEntry(string.Format("{0} deleted {1}!", wp.Identity.Name, typeName), EventLogEntryType.Information);
                            }
                        }
                        return rt;
                    }
                    catch (Exception ex)
                    {
                        using (var logger = new EventLog("Kingate"))
                        {
                            logger.Source = "Freshome";
                            if (EventLog.Exists(logger.Log) && EventLog.SourceExists(logger.Source))
                            {
                                while (ex.InnerException != null)
                                {
                                    ex = ex.InnerException;
                                }
                                logger.WriteEntry(ex.Message, EventLogEntryType.Error);
                            }
                        }
                        throw new FaultException(ex.Message);
                    }
                }
                else throw new FaultException(string.Format("用户无删除权限！"));
            }
            else throw new FaultException(string.Format("服务端配置错误！"));
        }
        public static T Read<T>(Func<T> funcall) where T : class
        {
            try
            {
                return funcall();
            }
            catch (Exception ex)
            {
                using (var logger = new EventLog("Kingate"))
                {
                    logger.Source = "Freshome";
                    if (EventLog.Exists(logger.Log) && EventLog.SourceExists(logger.Source))
                    {
                        while (ex.InnerException != null)
                        {
                            ex = ex.InnerException;
                        }
                        logger.WriteEntry(ex.Message, EventLogEntryType.Error);
                    }
                }
                throw new FaultException(ex.Message);
            }
        }
    }
}